//
//  HealthStatisticsViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/25.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "HealthStatisticsViewController.h"
#import "DetailHealthReportsViewController.h"

@interface HealthStatisticsViewController ()
@property (strong, nonatomic) NSMutableDictionary *dicModel;
@end

@implementation HealthStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    [self getRankingList];
    self.tableView.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 数据请求
- (void)getRankingList{
    [SVProgressHUD showWithStatus:@"获取健康报告统计数据.." maskType:SVProgressHUDMaskTypeClear];
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"YYYY-MM-dd"];
    NSDate* date = [formater dateFromString:self.startDateString];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *startDate = [date  dateByAddingTimeInterval: interval];
    int i = 6;
    NSDate *endDate = [NSDate dateWithTimeInterval:24*60*60*i sinceDate:startDate];
    NSString *startDateStr =[formater stringFromDate:startDate];
    NSString *endDateStr =[formater stringFromDate:endDate];
    NSString *user_id = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:user_id forKey:@"user_id"];
    [params setValue:startDateStr forKey:@"start_date"];
    [params setValue:endDateStr forKey:@"end_date"];
    [[BaseDataRequest alloc] getStatistice:params withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            _dicModel = [responseObject objectForKey:@"result"];
            [self.tableView reloadData];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    }];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 5;
    }else if(section == 1){
        return 5;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        NSString *identifier = @"HealthStatisticsCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
        }
        UILabel *labOne = [cell viewWithTag:1];
        UILabel *labTwo = [cell viewWithTag:2];
        NSArray *activity = [_dicModel objectForKey:@"warn"];
        if (indexPath.row == 0) {
            labOne.text = @"预警级别";
            labTwo.text = @"次数";
        }else if (indexPath.row == 1){
            labOne.attributedText = [self setLabelText:@" 一级提示" image:[UIImage imageNamed:@"earlywarning1"]];
            labTwo.text = activity[0];
        }else if (indexPath.row == 2){
            labOne.attributedText = [self setLabelText:@" 二级提示" image:[UIImage imageNamed:@"earlywarning2"]];
            labTwo.text = activity[1];
        }else if (indexPath.row == 3){
            labOne.attributedText = [self setLabelText:@" 三级预警" image:[UIImage imageNamed:@"earlywarning3"]];
            labTwo.text = activity[2];
        }else{
            labOne.attributedText = [self setLabelText:@" 一键呼叫" image:[UIImage imageNamed:@"earlywarningsos"]];
            labTwo.text = activity[3];
        }
        return cell;
    }else if (indexPath.section == 1){
        NSString *identifier = @"HealthStatisticsCellT";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
        }
        UILabel *labOne = [cell viewWithTag:1];
        UILabel *labTwo = [cell viewWithTag:2];
        UILabel *labS = [cell viewWithTag:3];
        UILabel *labF = [cell viewWithTag:4];
        if (indexPath.row == 0) {
            labOne.text = @"健康数据";
            labTwo.text = @"最大值";
            labS.text = @"最小值";
            labF.text = @"平均值";
        }else if (indexPath.row == 1){
            NSArray *heartrate = [_dicModel objectForKey:@"heartrate"];
            labOne.attributedText = [self setLabelText:@"心率(次/分)" perUnitLength:5];
            labTwo.text = heartrate[0];
            labS.text = heartrate[1];
            labF.text = heartrate[2];
        }else if (indexPath.row == 2){
            NSArray *spo2 = [_dicModel objectForKey:@"spo2"];
            labOne.attributedText = [self setLabelText:@"血氧(%)" perUnitLength:3];
            labTwo.text = spo2[0];
            labS.text = spo2[1];
            labF.text = spo2[2];
        }else if (indexPath.row == 3){
            NSArray *activity = [_dicModel objectForKey:@"activity"];
            labOne.attributedText = [self setLabelText:@"计步(步)" perUnitLength:3];
            labTwo.text = activity[0];
            labS.text = activity[1];
            labF.text = activity[2];
        }else{
            NSArray *sleep = [_dicModel objectForKey:@"sleep_total_time"];
            labOne.attributedText = [self setLabelText:@"睡眠(小时)" perUnitLength:4];
            labTwo.text = [NSString stringWithFormat:@"%.1f",[sleep[0] intValue]/60.0];
            labS.text = [NSString stringWithFormat:@"%.1f",[sleep[1] intValue]/60.0];
            labF.text = [NSString stringWithFormat:@"%.1f",[sleep[2] intValue]/60.0];
        }
        return cell;
    }else{
        NSString *identifier = @"HealthStatisticsCellS";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
        }
        UILabel *labOne = [cell viewWithTag:1];
        labOne.layer.cornerRadius = 8;
        labOne.layer.masksToBounds = YES;
        NSString *isdata = [_dicModel objectForKey:@"isdata"];
        if ([isdata isEqualToString:@"no"]) {
            labOne.backgroundColor = [UIColor grayColor];
        }
        return cell;
    }

}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        NSString *isdata = [_dicModel objectForKey:@"isdata"];
        if ([isdata isEqualToString:@"no"]) {
            return;
        }
        DetailHealthReportsViewController *detailReport = [[DetailHealthReportsViewController alloc]init];
        detailReport.startDateString = self.startDateString;
        detailReport.title = self.title;
        [self.navigationController pushViewController:detailReport animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];//创建一个视图
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH , 30)];
    headerLabel.font = [UIFont boldSystemFontOfSize:18.0];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    if (section == 0) {
       headerLabel.text = @"本周预警次数统计";
    }else if (section == 1){
        headerLabel.text = @"本周健康数据统计";
    }else{
        headerLabel.text = @"";
    }
    [headerView addSubview:headerLabel];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 60;
    }else if (section == 1){
        return 60;
    }else{
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 4) {
            return 41;
        }else{
            return 40;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 4) {
            return 41;
        }else{
            return 40;
        }
    }else{
        return 70;
    }
}

#pragma mark - 
- (NSMutableAttributedString *)setLabelText:(NSString *)text perUnitLength:(int)length{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:text];
    [attributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:11.0]
                          range:NSMakeRange(text.length - length, length)];
    return attributedStr;
}

- (NSMutableAttributedString *)setLabelText:(NSString *)title image:(UIImage *)image{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:title];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = image;
    attach.bounds = CGRectMake(0, -2, 17, 17);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    [string insertAttributedString:attachString atIndex:0];
    return string;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
